module.exports = {

  //加载中
  loading(msg) {

    uni.showLoading({
      title: msg || '正在加载中',
      duration: 1000
    });

    uni.showNavigationBarLoading({
      duration: 2000
    });
  },
  //隐藏加载中
  hideloading() {
    uni.hideNavigationBarLoading()
    uni.hideLoading()
  },
  chenggong(msg, callback = '') {
    uni.showToast({
      title: msg,
      icon: 'success',
      duration: 1500,
      success: function() {

        if (callback) {
          setTimeout(function() {
            //要延时执行的代码
            callback()
          }, 1000) //延迟时间
        }

      },
    })
  },
  shibai(msg, callback = '') {
    uni.showToast({
      title: msg,
      // icon: 'error',
      icon: 'none',
      duration: 1500,
      success: function() {
        if (callback) {
          setTimeout(function() {
            //要延时执行的代码
            callback()
          }, 1000) //延迟时间
        }
      },
    });
    return false;
  },
  //校验邮箱格式
  checkEmail: function(email) {
    return RegExp(
        /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/)
        .test(email);
  }
  //校验手机格式
  ,
  checkMobile(mobile) {
    return RegExp(/^1[3456789]\d{9}$/).test(mobile);
  },



  getDiffTime(end_date) {

    // dq_date 当前时间
    // end_date 截止时间
    end_date = end_date
    var timestamp = Date.parse(new Date()) / 1000
    dq_date = timestamp


    var subtime = end_date - dq_date; //计算时间差,并将毫秒转化为秒
    var days = parseInt(subtime / 86400); //天  24*60*60*1000
    var hours = parseInt(subtime / 3600) - 24 * days; //小时  60*60  总小时数-过去小时数=现在小时数
    var mins = parseInt(subtime % 3600 / 60); //分钟 - (day*24)  以60秒为一整份  取余 剩下秒数 秒数/60就是分钟数
    var secs = parseInt(subtime % 60); //以60秒为一整份  取余  剩下秒数

    if (dq_date > end_date) {
      days = 0
      hours = 0
      mins = 0
      secs = 0
    }

    var obj = {};
    obj.d = days;
    obj.h = hours;
    obj.m = mins;
    obj.s = secs;

    return obj;
  },
  /*转为多久前*/
  formatMsgTime(timespan) {
    var dateTime = new Date(timespan * 1000); // 将传进来的字符串或者毫秒转为标准时间
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    // var second = dateTime.getSeconds()
    var millisecond = dateTime.getTime(); // 将当前编辑的时间转换为毫秒
    var now = new Date(); // 获取本机当前的时间
    var nowNew = now.getTime(); // 将本机的时间转换为毫秒
    var milliseconds = 0;
    var timeSpanStr;
    milliseconds = nowNew - millisecond;
    if (milliseconds <= 1000 * 60 * 1) {
      // 小于一分钟展示为刚刚
      timeSpanStr = "刚刚";
    } else if (
        1000 * 60 * 1 < milliseconds &&
        milliseconds <= 1000 * 60 * 60
    ) {
      // 大于一分钟小于一小时展示为分钟
      timeSpanStr = Math.round(milliseconds / (1000 * 60)) + "分钟前";
    } else if (
        1000 * 60 * 60 * 1 < milliseconds &&
        milliseconds <= 1000 * 60 * 60 * 24
    ) {
      // 大于一小时小于一天展示为小时
      timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + "小时前";
    } else if (
        1000 * 60 * 60 * 24 < milliseconds &&
        milliseconds <= 1000 * 60 * 60 * 24 * 15
    ) {
      // 大于一天小于十五天展示位天
      timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + "天前";
    } else if (
        milliseconds > 1000 * 60 * 60 * 24 * 15 &&
        year === now.getFullYear()
    ) {
      timeSpanStr = month + "-" + day + " " + hour + ":" + minute;
    } else {
      timeSpanStr =
          year + "-" + month + "-" + day + " " + hour + ":" + minute;
    }
    return timeSpanStr;
  },


  timestampToTime(timestamp, type = 1) {
    // 时间戳为10位需*1000，时间戳为13位不需乘1000
    const time = String(timestamp).length == 10 ? timestamp * 1000 : timestamp;
    var date = new Date(time);
    var Y = date.getFullYear() + "-";
    var M =
        (date.getMonth() + 1 < 10 ?
            "0" + (date.getMonth() + 1) :
            date.getMonth() + 1) + "-";
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    var h = date.getHours() + ":";
    var m = date.getMinutes() + ":";
    var s = date.getSeconds();

    if (type == 1) {
      return Y + M + D;
    }
    return Y + M + D + h + m + s;
  },


  // 数字换算
  graceNumber(number) {
    if (number == 0) {
      return "0";
    } else if (number > 999 && number <= 9999) {
      return (number / 1000).toFixed(1) + 'k';
    } else if (number > 9999 && number <= 99999) {
      return (number / 10000).toFixed(1) + 'w';
    } else if (number > 99999) {
      return "10w+";
    }
    return number;

  },

  // 图片压缩递归，小于2M跳出

  // 图片压缩递归，小于2M跳出

  imageCompress(file) {
    return new Promise((resolve, reject) => {
      let {
        size,
        path
      } = file
      let type = path.split(".")[1]
      //大于1M进行压缩，
      console.log('大小' + size)
      if (size < (1024 * 1024 * 5)) {
        resolve(path)
        return false
      }

      uni.compressImage({
        src: path,
        quality: 50,
        success: res => {
          let newPath = res.tempFilePath + type
          let newName = res.tempFilePath.split("/")[res.tempFilePath.split("/")
                  .length - 1] +
              type
          resolve(res.tempFilePath)
        }
      })

    })

  },

  /* 全局处理编辑器的优化 */
  rep_desc(desc) {
    if (!desc) {
      return
    }
    var imgurl = 'https://fuzhuang2403.vhost.chengzhanheng.cn/';
    desc = desc.replace(/style/g, "class")

    if (desc.indexOf(imgurl) != -1) {

    } else {
      desc = desc.replace(/src="\//g, 'src="' + imgurl)
      desc = desc.replace(/href="\//g, 'href="' + imgurl)
    }
    desc = desc.replace(/\<img/gi, '<img style="max-width:100%;width:100%"')

    return desc;
  },


  // 复制
  coly_text(text) {

    var that = this
    if (!text) {
      this.shibai('未识别到文字');
    }

    wx.setClipboardData({
      data: text,
      showToast: false,
      success: function() {
        that.chenggong('复制成功');
      }
    });

  },


  /* 全局方法 拨打手机号 */
  callTel(tel = '') {

    if (!tel) {
      this.shibai('手机号有误');
    }
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },

  leftClick() {
    // 定义您的返回逻辑
    // 例如返回上一页
    uni.navigateBack({
      delta: 1
    });
  },

  // 在调用页面中的方法
  openfile(filePath) {
    // 跳转到包含 <web-view> 的页面，并传递文件路径作为参数

    // 手机只支持pdf
    uni.downloadFile({
      url: filePath, //文件的路径
      success: function(res) {
        var filePath = res.tempFilePath;
        uni.openDocument({
          filePath: filePath,
          showMenu: true,
          success: function(res) {
            console.log('打开文档成功');
          },
          fail: function(error) {
            console.log(error);
          }
        });
      }
    });


    // uni.navigateTo({
    // 	url: '/pages/tion/webview?filePath=' + encodeURIComponent(filePath)
    // });
  }




}